package com.rykj.qxj.monitor.rest;

import com.rykj.qxj.monitor.quartz.QuartzManager;
import com.rykj.qxj.monitor.util.Result;
import com.rykj.qxj.server.model.entity.Task;
import com.rykj.qxj.server.util.object.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Log4j
@RestController
@RequestMapping("/v1/quartz")
@Api(tags="quartz任务管理")
public class QuartzController {

    @PostMapping("/addJob")
    @ApiOperation(value="添加任务",notes ="添加任务")
    public Result addJob(HttpServletRequest request){
        try{
            String taskName= request.getParameter("taskName");
            String taskId =request.getParameter("taskId");
            String executeRate = request.getParameter("executeRate");
            QuartzManager.addJob(taskName, Integer.valueOf(taskId),executeRate);
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "添加失败");
        }
    }

    @PostMapping("/modifyJobTime")
    @ApiOperation(value="修改任务",notes ="修改任务")
    public Result modifyJobTime(HttpServletRequest request){
        try{
            String taskName= request.getParameter("taskName");
            String taskId =request.getParameter("taskId");
            String executeRate = request.getParameter("executeRate");
            QuartzManager.modifyJobTime(taskName, Integer.valueOf(taskId),executeRate);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }

    @GetMapping("/removeJob")
    @ApiOperation(value="删除任务",notes ="删除任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskName", value = "任务名称",required = true,paramType = "query")
    })
    public Result removeJob( @RequestParam(value = "taskName") String taskName){
        try{
            QuartzManager.removeJob(taskName);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }


    @GetMapping("/startOrShutdownJob")
    @ApiOperation(value="禁用或启用任务",notes ="禁用或启用任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskStatus", value = "任务状态: 启用enable，禁止disable",required = true,paramType = "query"),
            @ApiImplicitParam(name = "taskName", value = "任务名称",required = true,paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "任务id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "time", value = "任务执行频率",required = true,paramType = "query")

    })
    public Result startOrShutdownJob(@RequestParam(value = "taskStatus") String taskStatus,
                                    @RequestParam(value = "taskName") String taskName,
                                     @RequestParam(value = "taskId") Integer taskId,
                                     @RequestParam(value = "time") String time){
        try{
            if("enable".equals(taskStatus)){
                QuartzManager.addJob(taskName,taskId,time);
            }else if("disable".equals(taskStatus)){
                QuartzManager.removeJob(taskName);
            }
            return new Result(true,"操作成功");
        }catch (Exception e){
            log.error("操作失败", e);
            return new Result(false, "操作失败");
        }
    }


}
